<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld :msg="$store.state.count"/>
    <p>{{$store.state.userName}}</p>
    <a @click="addCount()">增加数字</a>
    <br>
    <a @click="lessCount()">减少数字</a>
    <br>
    <input type="text" v-model="userName">
    <button @click="updateName()">保存名字</button>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'Home',
  components: {
    HelloWorld
  },
  data() {
    return {
      userName: '',
    }
  },
  created() {
    this.userName = this.$store.state.userName;
  },
  methods: {
    addCount() {
      this.$store.commit('addCount');
    },
    lessCount() {
      this.$store.commit('lessCount');
    },
    updateName() {
      this.$store.dispatch('updateUN', this.userName)
    }
  },
}
</script>
